﻿@page "/forms/default-form-bound-dictionary-parameter"
@using Microsoft.AspNetCore.Components.Forms

<h2>Default form with dictionary bound parameter</h2>

<EditForm Enhance Model="Model" method="POST" OnValidSubmit="DisplayCustomer" FormName="someform">
    <ValidationSummary />
    <div>
        <label for="Model.Name">
            Name:
            <InputText @bind-Value="@(Model["Name"])" />
        </label>
    </div>
    <div>
        <label for="Model.Email">
            Email:
            <InputText @bind-Value="@(Model["Email"])" />
        </label>
    </div>
    <div>
        <label for="Model.IsPreferred">
            Preferred:
            <InputText @bind-Value="@(Model["IsPreferred"])" />
        </label>
    </div>
    <input id="send" type="submit" value="Send" />
</EditForm>

@if (_shouldDisplayCustomer)
{
    <p id="name">Customer: @Model["Name"]</p>
    <p id="email">Email: @Model["Email"]</p>
    <p id="preferred">Preferred: @Model["IsPreferred"]</p>
}

@code {

    bool _shouldDisplayCustomer = false;

    public void DisplayCustomer() => _shouldDisplayCustomer = true;

    [SupplyParameterFromForm] public Dictionary<string, string> Model { get; set; }

    [CascadingParameter] public FormMappingContext Context { get; set; }

    protected override void OnInitialized() => Model ??= new Dictionary<string, string>()
    {
        ["Name"] = "",
        ["Email"] = "",
        ["IsPreferred"] = "",
    };
}
